// $Id: CustomAttributesEditorForm.cs 64 2008-05-06 18:29:47Z nvivo $

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBLGen.UI.Design
{
	public partial class CustomAttributesEditorForm : Form
	{
		public CustomAttributesEditorForm(CustomAttributes ca)
		{
			if (ca == null)
				throw new ArgumentNullException("ca");

			InitializeComponent();

			_customAttributes = ca.Clone();
		}

		private CustomAttributes _customAttributes;

		public CustomAttributes CustomAttributes
		{
			get
			{
				return _customAttributes;
			}
		}

		private void BindItems()
		{
			BindingSource bs = new BindingSource();
			bs.DataSource = CustomAttributes;

			lbItems.DataSource = bs;
		}

		private void CustomAttributesEditorForm_Load(object sender, EventArgs e)
		{
			BindItems();

			lbItems_SelectedIndexChanged(this, EventArgs.Empty);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			using (NewAttributeForm frm = new NewAttributeForm(CustomAttributes))
			{
				if (frm.ShowDialog() == DialogResult.OK)
				{
					CustomAttributes.Add(frm.NewName, String.Empty);
					BindItems();
					lbItems.SelectedIndex = lbItems.Items.Count - 1;
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
			Close();
		}

		private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lbItems.SelectedIndex >= 0)
			{
				KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)lbItems.SelectedItem;

				txtValue.Text = kvp.Value;
				txtValue.Enabled = true;
				btnRemove.Enabled = true;
			}
			else
			{
				txtValue.Clear();
				txtValue.Enabled = false;
				btnRemove.Enabled = false;
			}
		}

		private void lbItems_Format(object sender, ListControlConvertEventArgs e)
		{
			KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)e.ListItem;
			e.Value = kvp.Key;
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			if (lbItems.SelectedIndex >= 0)
			{
				if (MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					KeyValuePair<string, string> kvp = GetSelectedPair();
					CustomAttributes.Remove(kvp.Key);
					BindItems();
					lbItems_SelectedIndexChanged(sender, e);
				}
			}
		}

		private void txtValue_Leave(object sender, EventArgs e)
		{
			if (lbItems.SelectedIndex >= 0)
			{
				KeyValuePair<string, string> kvp = GetSelectedPair();
				CustomAttributes[kvp.Key] = txtValue.Text;
				BindItems();
			}
		}

		private KeyValuePair<string, string> GetSelectedPair()
		{
			return (KeyValuePair<string, string>)lbItems.SelectedItem;
		}
	}
}
